export let playerTrack = $("#player-track"),
  bgArtwork = $('#bg-artwork'),
  bgArtworkUrl,
  albumName = $('#album-name'),
  trackName = $('#track-name'),
  albumArt = $('#album-art'),
  sArea = $('#s-area'),
  seekBar = $('#seek-bar'),
  trackTime = $('#track-time'),
  insTime = $('#ins-time'),
  sHover = $('#s-hover'),
  playPauseButton = $("#play-pause-button"),
  i = playPauseButton.find('i'),
  tProgress = $('#current-time'),
  tTime = $('#track-length'),
  seekT = 0, seekLoc = 0, seekBarPos = 0, cM = 0,
  ctMinutes = 0, ctSeconds = 0, curMinutes = 0,
  curSeconds = 0, durMinutes = 0, durSeconds = 0,
  playProgress = 0, bTime = 0, nTime = 0,
  buffInterval = null,
  tFlag = false,

  albums = ['Dawn', 'Me & You', 'Electro Boy', 'Home', 'Proxy (Original Mix)'],
  trackNames = ['Skylike - Dawn', 'Alex Skrindo - Me & You', 'Kaaze - Electro Boy', 'Jordan Schor - Home',
    'Martin Garrix - Proxy'
  ],
  albumArtworks = ['_1', '_2', '_3', '_4', '_5'],
  trackUrl = [
    'http://dev.kingyin.club:9000/cloud/Rom%E9%82%A2%E9%94%90%20-%20%E6%98%9F%E6%B2%B3%E4%B8%87%E9%87%8C.mp3',
    'mp3/2.mp3', 'mp3/1.mp3', 'mp3/2.mp3', 'mp3/1.mp3'
  ],

  playPreviousTrackButton = $('#play-previous'),
  playNextTrackButton = $('#play-next'),
  currIndex = -1,
  audio = new Audio(),
  currAlbum,
  currTrackName,
  currArtwork,
  avater = false;

  export function setTFlag(val) {
    tFlag = val;
  }

  export function setPlayProgress(val) {
    playProgress = val
  }

  export function setDurSeconds(val) {
    durSeconds = val;
  }

  export function setDurMinutes(val) {
    durMinutes = val;
  }

  export function setCurSeconds(val) {
    curSeconds = val;
  }

  export function setCtSeconds(val) {
    ctSeconds = val;
  }

  export function setCurMinutes(val) {
    curMinutes = val;
  }

  export function setCtMinutes(val) {
    ctMinutes = val;
  }

  export function setCM(val) {
    cM = val
  }

  export function setSeekBarPos(val) {
    seekBarPos = val;
  }

  export function setSeekLoc(val) {
    seekLoc = val
  }

  export function setSeekT(val) {
    seekT = val
  }

  export function setBgArtworkUrl(val) {
    bgArtworkUrl = val
  }

  export function setCurrArtwork(val) {
    currArtwork = val;
  }

  export function setCurrTrackName(val) {
    currTrackName = val;
  }

  export function setCurrAlbum(val) {
    currAlbum = val;
  }

  export function setCurrIndex(val) {
    currIndex = val
  }

  export function setBuffInterval(val) {
    buffInterval = val;
  }
  export function setBtime(val) {
    bTime = val;
  }
  export function setNtime(val) {
    nTime = val;
  }

export function playPause() {
  setTimeout(function() {
    if (audio.paused) {
      playerTrack.addClass('active');
      albumArt.addClass('active');
      checkBuffering();
      i.attr('class', 'fas fa-pause');
      audio.play();
    } else {
      
      playerTrack.removeClass('active');
      albumArt.removeClass('active');
      clearInterval(buffInterval);
      albumArt.removeClass('buffering');
      i.attr('class', 'fas fa-play');
      audio.pause();
    }
  }, 300);
}


export function showHover(event) {
  seekBarPos = sArea.offset();
  seekT = event.clientX - seekBarPos.left;
  seekLoc = audio.duration * (seekT / sArea.outerWidth());

  sHover.width(seekT);

  cM = seekLoc / 60;

  ctMinutes = Math.floor(cM);
  ctSeconds = Math.floor(seekLoc - ctMinutes * 60);

  if ((ctMinutes < 0) || (ctSeconds < 0))
    return;

  if ((ctMinutes < 0) || (ctSeconds < 0))
    return;

  if (ctMinutes < 10)
    ctMinutes = '0' + ctMinutes;
  if (ctSeconds < 10)
    ctSeconds = '0' + ctSeconds;

  if (isNaN(ctMinutes) || isNaN(ctSeconds))
    insTime.text('--:--');
  else
    insTime.text(ctMinutes + ':' + ctSeconds);

  insTime.css({
    'left': seekT,
    'margin-left': '-21px'
  }).fadeIn(0);

}

export function hideHover() {
  sHover.width(0);
  insTime.text('00:00').css({
    'left': '0px',
    'margin-left': '0px'
  }).fadeOut(0);
}

export function playFromClickedPos() {
  audio.currentTime = seekLoc;
  seekBar.width(seekT);
  hideHover();
}

export function updateCurrTime() {
  nTime = new Date();
  nTime = nTime.getTime();

  if (!tFlag) {
    tFlag = true;
    trackTime.addClass('active');
  }

  // 当前分钟
  curMinutes = Math.floor(audio.currentTime / 60);
  // 当前秒
  curSeconds = Math.floor(audio.currentTime - curMinutes * 60);

  // 总分钟
  durMinutes = Math.floor(audio.duration / 60);
  // 总秒
  durSeconds = Math.floor(audio.duration - durMinutes * 60);

  playProgress = (audio.currentTime / audio.duration) * 100;

  // 格式化
  if (curMinutes < 10)
    curMinutes = '0' + curMinutes;
  if (curSeconds < 10)
    curSeconds = '0' + curSeconds;

  if (durMinutes < 10)
    durMinutes = '0' + durMinutes;
  if (durSeconds < 10)
    durSeconds = '0' + durSeconds;

  if (isNaN(curMinutes) || isNaN(curSeconds))
    tProgress.text('00:00');
  else
    tProgress.text(curMinutes + ':' + curSeconds);

  if (isNaN(durMinutes) || isNaN(durSeconds))
    tTime.text('00:00');
  else
    tTime.text(durMinutes + ':' + durSeconds);

  if (isNaN(curMinutes) || isNaN(curSeconds) || isNaN(durMinutes) || isNaN(durSeconds))
    trackTime.removeClass('active');
  else
    trackTime.addClass('active');


  seekBar.width(playProgress + '%');

  if (playProgress == 100) {
    i.attr('class', 'fa fa-play');
    seekBar.width(0);
    tProgress.text('00:00');
    albumArt.removeClass('buffering').removeClass('active');
    clearInterval(buffInterval);
  }
}

export function checkBuffering() {
  clearInterval(buffInterval);
  buffInterval = setInterval(function() {
    if ((nTime == 0) || (bTime - nTime) > 1000)
      albumArt.addClass('buffering');
    else
      albumArt.removeClass('buffering');

    bTime = new Date();
    bTime = bTime.getTime();

  }, 100);
}

export function selectTrack(flag) {
  if (flag == 0 || flag == 1)
    ++currIndex;
  else
    --currIndex;
  if ((currIndex > -1) && (currIndex < albumArtworks.length)) {
    if (flag == 0)
      i.attr('class', 'fa fa-play');
    else {
      albumArt.removeClass('buffering');
      i.attr('class', 'fa fa-pause');
    }

    seekBar.width(0);
    trackTime.removeClass('active');
    tProgress.text('00:00');
    tTime.text('00:00');

    currAlbum = albums[currIndex];
    currTrackName = trackNames[currIndex];

    currArtwork = '_1';
    if (avater) {
      currArtwork = albumArtworks[currIndex];
    } else {
      currArtwork = '_1';
    }

    audio.src = trackUrl[currIndex];

    nTime = 0;
    bTime = new Date();
    bTime = bTime.getTime();

    if (flag != 0) {
      audio.play();
      playerTrack.addClass('active');
      albumArt.addClass('active');

      clearInterval(buffInterval);
      checkBuffering();
    }

    albumName.text(currAlbum);
    trackName.text(currTrackName);
    albumArt.find('img.active').removeClass('active');
    $('#' + currArtwork).addClass('active');

    bgArtworkUrl = $('#' + currArtwork).attr('src');

    bgArtwork.css({
      'background-image': 'url(' + bgArtworkUrl + ')'
    });
  } else {
    if (flag == 0 || flag == 1)
      --currIndex;
    else
      ++currIndex;
  }
}

export function initPlayer() {


  selectTrack(0);

  audio.loop = false;

  playPauseButton.on('click', playPause);

  sArea.mousemove(function(event) {
    showHover(event);
  });

  sArea.mouseout(hideHover);

  sArea.on('click', playFromClickedPos);

  $(audio).on('timeupdate', updateCurrTime);

  playPreviousTrackButton.on('click', function() {
    selectTrack(-1);
  });
  playNextTrackButton.on('click', function() {
    selectTrack(1);
  });
}

export function initMusic(data) {
  albums = data.name;
  trackNames = data.title;
  trackUrl = data.url;
}

export function music() {
  return audio;
}
